package com.cxs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cxs.entity.Clazz;
import com.cxs.entity.College;
import com.cxs.entity.Student;
import com.cxs.service.ClazzService;
import com.cxs.service.CollegeService;
import com.cxs.service.SeatOrderService;
import com.cxs.service.SeatService;
import com.cxs.service.StudentService;
import com.cxs.entity.vo.AdminLoginVO;
import com.cxs.entity.vo.Result;
import com.cxs.entity.vo.SeatOrderVO;
import com.cxs.entity.vo.StudentLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.List;

/*
 * @Project:seat-subscribe-sys
 * @Author:cxs
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
public class IndexController {

    @Autowired
    private SeatService seatService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private SeatOrderService seatOrderService;

    @Autowired
    private CollegeService collegeService;

    @Autowired
    private ClazzService clazzService;

    @RequestMapping("/toIndex")
    public String toIndex(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
//        AdminLoginVO vo = new AdminLoginVO();
//        vo.setId(1);
//        vo.setUsername("admin");
//        vo.setRole("管理员");
//        session.setAttribute("loginUser", vo);

//        StudentLoginVO vo = new StudentLoginVO();
//        vo.setSno(20230001);
//        vo.setClazzId(1);
//        vo.setRole("学生");
//        session.setAttribute("loginUser", vo);
        Object user = session.getAttribute("loginUser");
        if (null != user) {
            if (user instanceof AdminLoginVO) {
                AdminLoginVO loginVO = (AdminLoginVO) user;
                model.addAttribute("user",loginVO);
                return "redirect:/admin/";
            } else if (user instanceof StudentLoginVO) {
                StudentLoginVO loginVO = (StudentLoginVO) user;
                model.addAttribute("user", loginVO);
                return "redirect:/student/";
            } else {
                return "redirect:/auth/login";
            }
        } else {
            return "redirect:/auth/login";
        }
    }

    @RequestMapping("/welcome")
    public ModelAndView welcome(ModelAndView mv, HttpSession session, HttpServletRequest request) {
        Object user = session.getAttribute("loginUser");
        if (null != user) {
            if (user instanceof StudentLoginVO) {
                StudentLoginVO loginVO = (StudentLoginVO) user;
                List<SeatOrderVO> list = seatOrderService.getReservationBySno(loginVO.getSno(), LocalDate.now());
                mv.addObject("list", list);
                mv.addObject("user", loginVO);
            } else {
                AdminLoginVO loginVO = (AdminLoginVO) user;
                mv.addObject("user", loginVO);
            }
        }
        mv.setViewName("welcome");
        return mv;
    }

    @RequestMapping("/toRegister")
    public ModelAndView toRegister(ModelAndView mv, HttpSession session, HttpServletRequest request) {

        mv.setViewName("register");
        return mv;
    }

    @RequestMapping("/getCollege")
    @ResponseBody
    public Result getCollege(HttpServletRequest request) {
        Result r = new Result(200, "OK");
        List<College> list = collegeService.list(null);
        r.setData(list);
        return r;
    }

    @RequestMapping("/register")
    @ResponseBody
    public Result register(Student student, HttpServletRequest request) {
        Result r = new Result(200, "OK");
        studentService.save(student);
        r.setData(student);
        return r;
    }

    @RequestMapping("/getClazz")
    @ResponseBody
    public Result getClazz(Integer id, HttpServletRequest request) {
        Result r = new Result(200, "OK");
        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Clazz::getCollegeId, id);
        List<Clazz> list = clazzService.list(queryWrapper);
        r.setData(list);
        return r;
    }

    @RequestMapping("/404")
    public ModelAndView error404(ModelAndView mv, HttpServletRequest request) {
        mv.setViewName("error/404");
        return mv;
    }
}
